package com.student.service.impl;

import com.student.bean.User;
import com.student.mapper.UserMapper;
import com.student.service.UserService;
import com.student.utils.SaltUtil;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    @Override
    public void register(User user) {
        String password = user.getLoginpassword();
        String salt = SaltUtil.getSalt();
        password = new Md5Hash(password, salt).toString();
        user.setLoginpassword(password);
        user.setSalt(salt);
        user.setRegistertime(new Date());
        System.out.println(user);
        //插入user表
        userMapper.insert(user);
        Integer userId = user.getUserid();
        //设置权限
        userMapper.insertRole(userId, 1);
    }

    @Override
    public List<User> selectUser(String loginname) {
        return userMapper.selectUser(loginname);
    }

    @Override
    public boolean updateUser(String nickname, String signature, String sex, Integer age, Integer phonenNumber, String area, Integer constellationId, String email, String loginname) {
        return userMapper.updatePerson1(nickname, signature, sex, age, phonenNumber, area, constellationId, email, loginname);
    }
}
